import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  rules: {
    houseInfo: [
      { required: true, message: '请填写报修房屋!' },
      // { pattern: /^(?:[\u4e00-\u9fa5·]{2,16})$/, message: '请检查业主姓名是否正确!' },
    ],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1\d{10}$/, message: '请检查手机号码是否正确!' },
    ],
    repairItemName: [{ required: true, message: '请选择维修项目!' }],
    appointment: [{ required: true, message: '请选择预约日期!' }],
    description: [{ required: true, message: '请输入问题描述!' }],
    attachment: [{ required: true, message: '请上传附件照片!' }],
  },
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    houseInfo: '',
    houseId: '',
    houseRepairList: [],
    repairItemName: '',
    repairItemId: '',
    id: undefined,
    mobile: '',
    appointment: '',
    description: '',
    attachment: [],
  },
  onLoad({ id }) {
    // console.log(e);
    if (id) this.getRepairDetail(id)
    this.getHouseList()
    this.getHouseRepair()
  },
  // 修改报修
  async getRepairDetail(id) {
    const res = await wx.http.get(`/repair/${id}`)
    console.log(res.data);
    this.setData({
      ...res.data
    })
  },
  // 提交保修
  async subRepair() {
    // 简单校验
    if (!this.validate()) return
    // console.log('ok');
    const { id, attachment, description, appointment, mobile, houseId, repairItemId } = this.data
    const res = await wx.http.post('/repair', {
      id,
      attachment,
      description,
      appointment,
      mobile,
      repairItemId,
      houseId,
    })
    console.log(res);
    if (res.code !== 10000) {
      return wx.utils.toast('在线报修失败!')
    }
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
  // 上传图片之前
  beforeRead(ev) {
    console.log(ev);
    const { file, callback } = ev.detail
    if (file.size > 1024 * 1024 * 1) {
      wx.utils.toast('文件大小不能超过1M')
      return callback(false)
    }
    callback(true)
  },

  // 删除照片
  uplodeDelete(ev) {
    this.setData({
      attachment: this.data.attachment.filter((ele, index) => index !== ev.detail.index)
    })
  },
  //afterRead 选择上传图片后处理的事件
  async afterRead(ev) {
    const res = await wx.http.upload('/upload', {
      name: 'file',
      filePath: ev.detail.file.tempFilePath
    })
    // console.log(res);
    this.setData({
      attachment: [...this.data.attachment, res.data]
    })
  },
  // 选择预约日期
  selectDate(ev) {
    // const date = new Date(ev.detail)
    // console.log(ev);
    // 替换格式
    // const nowDate = date.toLocaleDateString().replaceAll('/', '-')
    this.setData({
      appointment: wx.utils.formatDate(ev.detail),
      dateLayerVisible: false
    })
  },
  // 获取维修项目
  async getHouseRepair() {
    const res = await wx.http.get('/repairItem')
    console.log(res);
    this.setData({
      houseRepairList: res.data,
    })
  },
  // 获取保修房屋
  async getHouseList() {
    const res = await wx.http.get('/house')
    console.log(res);
    this.setData({
      houseList: res.data,
    })
  },
  // 选择保修房屋
  selectHouse(e) {
    console.log(e.detail);
    this.setData({
      houseInfo: e.detail.name,
      houseId: e.detail.id
    })
  },

  // 选择维修项目
  selectRepair(e) {
    console.log(e.detail);
    this.setData({
      repairItemName: e.detail.name,
      repairItemId: e.detail.id
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // goList() {
  //   wx.reLaunch({
  //     url: '/repair_pkg/pages/list/index',
  //   })
  // },
})
